﻿using OF.Component.BLL;
using OF.Component.Model;
using OF.Security;
using OF.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OpSystem.common
{
    public class BasePage : System.Web.UI.Page
    {
        /// <summary>
        /// 当前登陆用户
        /// </summary>
        public UserInfo CurrentUser;

        /// <summary>
        /// 系统编码
        /// </summary>
        public string SystemCode {
            get {
                return SiteUtility.GetAppSettings("SystemCode");
            }
        }
       

        protected override void OnPreLoad(EventArgs e)
        {
            base.OnPreLoad(e);
            //验证用户登陆
            ValidLogin();
          
        }

        #region 公共方法

        /// <summary>
        /// 获取参数（QueryString和Form）
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        protected string GetParam(string key)
        {
            string value = Request.QueryString[key];
            if (string.IsNullOrEmpty(value))
            {
                value = Request.Form[key];
            }
            return value;
        }

        /// <summary>
        /// 验证用户是否登陆
        /// </summary>
        protected bool ValidLogin()
        {
            //验证用户是否登陆
            if (!UserContext.IsAuthenticated)
            {
                return false;
            }
            CurrentUser = new UserInfo
            {
                UserId = UserContext.Current.UserID,
                UserName = UserContext.Current.UserName
            };
            return true;
        }

        #endregion

        /// <summary>
        /// 获取页面地址
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        protected string GetPageUrl(string page)
        {
            string url = Request.FilePath.Substring(0, Request.FilePath.LastIndexOf("/"));
            return string.Format("{0}/{1}", url, page);
        }

        /// <summary>
        /// 验证权限
        /// </summary>
        /// <param name="menuCode"></param>
        protected void CheckPageCode(string menuCode)
        {
            IList<AuthorityMenuInfo> menuList = AuthorityRoleMenuRelaOperate.GetRoleMenuList(CurrentUser.UserId, SystemCode);
            if (menuList.Count == 0 || menuList.Where(a => a.MenuCode == menuCode).ToList().Count == 0)
            {
                  Response.Redirect(Page.ResolveUrl("~/403.html"));
            }
        }
    }
}